/**
 * sns-正负向统计
 */

;
(function($, window, document, undefined) {
    var comChart;

    var SNSTrendStatistic = function(el, opts) {
        this.el = el;
        this.defaults = {
            theme: 'macarons',
            chartOption: {
                calculable: true,
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    data: ['正向', '负向', '中立']
                },
                series: [{
                    name: '访问来源',
                    type: 'pie',
                    radius: '55%',
                    center: ['50%', '50%'],
                    data: [{
                        value: 0,
                        name: '正向'
                    }, {
                        value: 0,
                        name: '负向'
                    }, {
                        value: 0,
                        name: '中立'
                    }]
                }]
            }
        };
        this.options = $.extend({}, this.defaults, opts);
    };

    SNSTrendStatistic.prototype = {
        init: function() {
            var me = this,
                el = me.el;

            if (el.attr('data-initialized') == 'true') {
                return me;
            }

            me.initChart();
            me.bindEvents();


            el.attr('data-initialized', 'true');
            return me;
        },


        /**
         * [bindEvents description]
         * @return {[type]} [description]
         */
        bindEvents: function() {
            var me = this,
                el = me.el;

        },


        /**
         * 格式化数据
         * @param  {[type]} data [description]
         * @return {[type]}      [description]
         */
        formatData: function(originData) {
            var me = this,
                el = me.el,
                chartOption = me.options.chartOption;


            // 初始化series
            chartOption.series[0].data[0].value = originData['positive'];
            chartOption.series[0].data[1].value = originData['negative'];
            chartOption.series[0].data[2].value = originData['neutral'];

            return chartOption;
        },

        /**
         * [initChart description]
         * @return {[type]} [description]
         */
        initChart: function() {
            var me = this,
                el = me.el,
                option = {};

            comChart = echarts.init(el[0], me.options.theme);
            option = me.formatData(me.options.data);
            comChart.setOption(option);


//            $.ajax({
//                url: $.fn.utils.env == 'dev' ? '/keywords/1/sns_trend_statistic.json' : '',
//                type: 'GET',
//                dataType: 'json',
//                data: {},
//                success: function(ret) {
//                    if (ret && ret.status) {
//                        var data = ret.data[0].sns_trend_statistic;
//
//                        option = me.formatData(data);
//                        comChart.setOption(option);
//                    } else {
//
//                    }
//                },
//                error: function(ret) {
//
//                }
//            });

        }
    };

    $.fn.SNSTrendStatistic = function(opts) {
        var me = this,
            el = me.el,
            comSNSTrendStatistic;

        // 如果没有被初始化过
        if (!(el && el.length && el.attr('data-initialized') != 'true')) {
            comSNSTrendStatistic = new SNSTrendStatistic(this, opts);
        }

        return comSNSTrendStatistic.init();
    }
})(jQuery, window, document);
